package com.coder4j.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.coder4j.entity.*;
import com.coder4j.mapper.STaskFlowMapper;
import com.coder4j.utils.ListUtils;
import com.coder4j.vo.*;
import com.coder4j.starter.web.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 任务流程表
 * @author keller
 * @date 2025-03-18 16:22:26
 */
@Service
@Slf4j
public class STaskFlowService extends BaseServiceImpl<STaskFlowMapper,STaskFlowEntity,STaskFlowVO> {

    @Resource
    private STaskFlowMapper mapper;

    @Resource
    private SControlService sControlService;

    @Resource
    private SDisplayInterfaceService sDisplayInterfaceService;

    @Resource
    private STaskFlowDetailService sTaskFlowDetailService;

    @Resource
    private SDisplayInterfaceDetailService sDisplayInterfaceDetailService;

    @Resource
    private SControlDetailService sControlDetailService;

    @Override
    public STaskFlowVO entity2Vo(STaskFlowEntity entity) {
        STaskFlowVO vo = new STaskFlowVO();
        BeanUtils.copyProperties(entity,vo);
        return vo;
    }

    @Override
    public STaskFlowEntity vo2Entity(STaskFlowVO vo) {
        STaskFlowEntity entity = new STaskFlowEntity();
        BeanUtils.copyProperties(vo, entity);
        return entity;
    }

    public List<MenuData> getMenuData(){
        List<MenuData> list = new ArrayList<>();
        STaskFlowQuery query = new STaskFlowQuery();
        List<STaskFlowEntity> entityList = this.baseQueryList(query);
        if(ListUtils.isEmpty(entityList)){
            return list;
        }

        Map<String, List<STaskFlowEntity>> map = entityList.stream().collect(Collectors.groupingBy(STaskFlowEntity::getTaskType));
        for (String type : map.keySet()) {
            MenuData data = new MenuData();
            data.setKey(type);
            data.setLabel(type);
            data.setChildren(new ArrayList<>());
            for (STaskFlowEntity entity : map.get(type)) {
                MenuData child = new MenuData();
                child.setKey(entity.getId()+"");
                child.setLabel(entity.getTaskName());
                data.getChildren().add(child);
            }
            list.add(data);
        }
        return list;
    }

    public List<Map<String, Object>> getTaskLabelList() {
        return mapper.selectListForAllLabel();
    }

    public Object sjtd() {
        Map<String,Object> respMap = new HashMap<>();


        List<JSONObject> list = new ArrayList<>();
        JSONObject entries1 = new JSONObject();
        entries1.putOnce("label","0.0-无视觉活动");
        entries1.putOnce("value","0.0");


        JSONObject entries2 = new JSONObject();
        entries2.putOnce("label","1.0-视觉发现（检测事件的发生）");
        entries2.putOnce("value","1.0");

        JSONObject entries3 = new JSONObject();
        entries3.putOnce("label","3.0-视觉检查（确认系统状态）");
        entries3.putOnce("value","3.0");

        JSONObject entries4 = new JSONObject();
        entries4.putOnce("label","4.0-视觉定位（选择注视位置）");
        entries4.putOnce("value","4.0");

        JSONObject entries5 = new JSONObject();
        entries5.putOnce("label","4.4-视觉追踪（保持注视位置）");
        entries5.putOnce("value","4.4");

        JSONObject entries6 = new JSONObject();
        entries6.putOnce("label","5.0-视觉区分（区分视觉差异)");
        entries6.putOnce("value","5.0");

        JSONObject entries7 = new JSONObject();
        entries7.putOnce("label","5.1-视觉阅读（文字、符号等）");
        entries7.putOnce("value","5.1");

        JSONObject entries8 = new JSONObject();
        entries8.putOnce("label","5.1-视觉扫描、搜索、监视（连续检索多个范围）");
        entries8.putOnce("value","5.1");

        list.add(entries1);
        list.add(entries2);
        list.add(entries3);
        list.add(entries4);
        list.add(entries5);
        list.add(entries6);
        list.add(entries7);
        respMap.put("sj", list);

        return tjtd(respMap);
    }

    public Object tjtd(Map<String,Object> respMap) {
        List<JSONObject> list = new ArrayList<>();
        JSONObject entries1 = new JSONObject();
        entries1.putOnce("label","0.0-无");
        entries1.putOnce("value","0.0");

        JSONObject entries2 = new JSONObject();
        entries2.putOnce("label","1.0-听觉发现（检测声音的发生）");
        entries2.putOnce("value","1.0");

        JSONObject entries3 = new JSONObject();
        entries3.putOnce("label","2.0-声音定位（常规判断/注意）");
        entries3.putOnce("value","2.0");

        JSONObject entries4 = new JSONObject();
        entries4.putOnce("label","3.0-理解语义内容（简单，1-2个词））");
        entries4.putOnce("value","3.0");

        JSONObject entries5 = new JSONObject();
        entries5.putOnce("label","4.2-声音选择（选择性注意）");
        entries5.putOnce("value","4.2");

        JSONObject entries6 = new JSONObject();
        entries6.putOnce("label","4.3-验证听觉反馈（检测预期的声音发生）");
        entries6.putOnce("value","4.3");

        JSONObject entries7 = new JSONObject();
        entries7.putOnce("label","6.0-理解语义内容（复杂，句子）");
        entries7.putOnce("value","6.0");

        JSONObject entries8 = new JSONObject();
        entries8.putOnce("label","6.6-辨别声音特性（听觉差异检测）");
        entries8.putOnce("value","6.6");

        JSONObject entries9 = new JSONObject();
        entries9.putOnce("label","7.0-理解声音的模式（幅值、频率等");
        entries9.putOnce("value","7.0");

        list.add(entries1);
        list.add(entries2);
        list.add(entries3);
        list.add(entries4);
        list.add(entries5);
        list.add(entries6);
        list.add(entries7);
        list.add(entries8);
        list.add(entries9);
        respMap.put("tj",list);
        return  rztd(respMap);

    }

    public Object rztd(Map<String,Object> respMap) {
        List<JSONObject> list = new ArrayList<>();
        JSONObject entries1 = new JSONObject();
        entries1.putOnce("label","0.0-无");
        entries1.putOnce("value","0.0");

        JSONObject entries2 = new JSONObject();
        entries2.putOnce("label","1.0-自动反应（简单反应）");
        entries2.putOnce("value","1.0");

        JSONObject entries3 = new JSONObject();
        entries3.putOnce("label","1.2-简单选择（开或关）");
        entries3.putOnce("value","1.2");

        JSONObject entries4 = new JSONObject();
        entries4.putOnce("label","4.6-评估/判断（考虑单方面）");
        entries4.putOnce("value","4.6");

        JSONObject entries5 = new JSONObject();
        entries5.putOnce("label","5.0-信号识别");
        entries5.putOnce("value","5.0");

        JSONObject entries6 = new JSONObject();
        entries6.putOnce("label","5.3-编码、解码、回忆");
        entries6.putOnce("value","5.3");

        JSONObject entries7 = new JSONObject();
        entries7.putOnce("label","6.8-评估/判断（考虑多个方面））");
        entries7.putOnce("value","6.8");

        JSONObject entries8 = new JSONObject();
        entries8.putOnce("label","7.0-估计、计算、转换");
        entries8.putOnce("value","7.0");


        list.add(entries1);
        list.add(entries2);
        list.add(entries3);
        list.add(entries4);
        list.add(entries5);
        list.add(entries6);
        list.add(entries7);
        list.add(entries8);

        respMap.put("rz",list);
        return  jsydtd( respMap);
    }

    public Object jsydtd(Map<String,Object> respMap) {
        List<JSONObject> list = new ArrayList<>();
        JSONObject entries1 = new JSONObject();
        entries1.putOnce("label","0.0-无");
        entries1.putOnce("value","0.0");

        JSONObject entries2 = new JSONObject();
        entries2.putOnce("label","2.2-离散操作（按钮、触发开关）");
        entries2.putOnce("value","2.2");

        JSONObject entries3 = new JSONObject();
        entries3.putOnce("label","2.6-连续调节（操纵杆，油门等）");
        entries3.putOnce("value","2.6");

        JSONObject entries4 = new JSONObject();
        entries4.putOnce("label","4.6-操纵（追踪，连贯操作）");
        entries4.putOnce("value","4.6");

        JSONObject entries5 = new JSONObject();
        entries5.putOnce("label","5.5-离散调节（旋转、垂直轮、杆位置）");
        entries5.putOnce("value","5.5");

        JSONObject entries6 = new JSONObject();
        entries6.putOnce("label","6.5-符号产生（写作）");
        entries6.putOnce("value","6.5");

        JSONObject entries7 = new JSONObject();
        entries7.putOnce("label","7.0-复杂串行离散操作（键盘输入等）");
        entries7.putOnce("value","7.0");


        list.add(entries1);
        list.add(entries2);
        list.add(entries3);
        list.add(entries4);
        list.add(entries5);
        list.add(entries6);
        respMap.put("jsyd",list);
       return taskTypeList(respMap);
        //return  respMap;
    }

    public Object taskTypeList(Map<String,Object> respMap) {
        List<STaskFlowEntity> sTaskFlowEntities = this.baseQueryList(new STaskFlowQuery());
        if (ListUtils.isEmpty(sTaskFlowEntities)) {
            return respMap;
        }
        // 去重 taskType
        Set<String> taskTypeSet = sTaskFlowEntities.stream()
                .map(STaskFlowEntity::getTaskType)
                .collect(Collectors.toSet());

        // 转换为 label-value 形式的列表
        List<Map<String, Object>> taskTypeLabels = taskTypeSet.stream()
                .map(taskType -> {
                    Map<String, Object> labelValueMap = new HashMap<>();
                    labelValueMap.put("label", taskType);
                    labelValueMap.put("value", taskType);
                    return labelValueMap;
                })
                .collect(Collectors.toList());
        respMap.put("taskType", taskTypeLabels);
        return Contro(respMap);
    }


    public Object Contro(Map<String,Object> respMap) {
        List<SControlEntity> sControlEntities = sControlService.baseQueryList(new SControlQuery());
        if (ListUtils.isEmpty(sControlEntities)) {
            return respMap;
        }
        // 去重 taskType
        Set<String> taskTypeSet = sControlEntities.stream()
                .map(SControlEntity::getControlType)
                .collect(Collectors.toSet());

        // 转换为 label-value 形式的列表
        List<Map<String, Object>> taskTypeLabels = taskTypeSet.stream()
                .map(taskType -> {
                    Map<String, Object> labelValueMap = new HashMap<>();
                    labelValueMap.put("label", taskType);
                    labelValueMap.put("value", taskType);
                    return labelValueMap;
                })
                .collect(Collectors.toList());
        respMap.put("control", taskTypeLabels);
        return display(respMap);
    }


    public Object display(Map<String,Object> respMap) {
        List<SDisplayInterfaceEntity> sDisplayInterfaceEntities = sDisplayInterfaceService.baseQueryList(new SDisplayInterfaceQuery());
        if (ListUtils.isEmpty(sDisplayInterfaceEntities)) {
            return respMap;
        }
        // 去重 taskType
        Set<String> taskTypeSet = sDisplayInterfaceEntities.stream()
                .map(SDisplayInterfaceEntity::getDisplayType)
                .collect(Collectors.toSet());

        // 转换为 label-value 形式的列表
        List<Map<String, Object>> taskTypeLabels = taskTypeSet.stream()
                .map(taskType -> {
                    Map<String, Object> labelValueMap = new HashMap<>();
                    labelValueMap.put("label", taskType);
                    labelValueMap.put("value", taskType);
                    return labelValueMap;
                })
                .collect(Collectors.toList());
        respMap.put("display", taskTypeLabels);
        return respMap;
    }



    /**
     * 获取当前任务的所有信息
     */
    public void getTaskAllMsg(Integer taskId,Integer controlId,Integer displayId){
        //获取任务信息
        STaskFlowEntity task = baseGetById(taskId);
        //获取任务步骤信息
        STaskFlowDetailQuery sTaskFlowDetailQuery = new STaskFlowDetailQuery();
        sTaskFlowDetailQuery.setTaskFlowId(taskId);
        List<STaskFlowDetailEntity> sTaskFlowDetailEntities = sTaskFlowDetailService.baseQueryList(sTaskFlowDetailQuery);
        //获取任务关联的显示界面信息
        SDisplayInterfaceQuery sDisplayInterfaceQuery = new SDisplayInterfaceQuery();
        sDisplayInterfaceQuery.setId(Long.parseLong(String.valueOf(displayId)));
        SDisplayInterfaceEntity sDisplayInterfaceEntity = sDisplayInterfaceService.baseQueryOne(sDisplayInterfaceQuery);
        //获取界面关联的详情
        SDisplayInterfaceDetailQuery sDisplayInterfaceDetailQuery = new SDisplayInterfaceDetailQuery();
        sDisplayInterfaceDetailQuery.setTaskFlow(taskId);
        sDisplayInterfaceDetailQuery.setDisplayInterfaceId(displayId);

        List<SDisplayInterfaceDetailVO> sDisplayInterfaceDetailVOS = sDisplayInterfaceDetailService.baseQueryVOList(sDisplayInterfaceDetailQuery);

        for (SDisplayInterfaceDetailVO sDisplayInterfaceDetailEntity : sDisplayInterfaceDetailVOS) {
            JSONObject area = JSONUtil.parseObj(sDisplayInterfaceDetailEntity.getInterestArea());
            Double x = area.getDouble("x");
            Double y = area.getDouble("y");
            Double distance = 0D;
            for (SDisplayInterfaceDetailVO displayInterfaceDetailEntity : sDisplayInterfaceDetailVOS) {
                if(!String.valueOf(displayInterfaceDetailEntity.getId()).equals(String.valueOf(sDisplayInterfaceDetailEntity.getId()))){
                    JSONObject area1 = JSONUtil.parseObj(displayInterfaceDetailEntity.getInterestArea());
                    Double x1 = area1.getDouble("x");
                    Double y1 = area1.getDouble("y");
                    // 计算两点之间的欧几里得距离
                    distance += Math.sqrt(Math.pow(x1 - x, 2) + Math.pow(y1 - y, 2));
                    // 可以在这里处理计算出的距离，例如保存到某个地方或打印出来
                    System.out.println("Distance between points: " + distance);
                }
            }
            sDisplayInterfaceDetailEntity.setDistance(distance);

        }
        //获取任务关联的控制界面
        SControlDetailQuery sControlDetailQuery = new SControlDetailQuery();
        sControlDetailQuery.setControlId(controlId);
        List<SControlDetailEntity> sControlDetailEntities = sControlDetailService.baseQueryList(sControlDetailQuery);


        JSONObject respJson = new JSONObject();
        respJson.putOnce("task",task);
        respJson.putOnce("taskDetail",sTaskFlowDetailEntities);
        respJson.putOnce("display",sDisplayInterfaceEntity);
        respJson.putOnce("displayDetail",sDisplayInterfaceDetailVOS);
        //respJson.putOnce("control",sDisplayInterfaceDetailEntities);
        respJson.putOnce("controlDetail",sControlDetailEntities);
        System.out.println(respJson);
    }
}
